<template>
  <section class="component mail-send-list">
    <!-- 数据列表处理 -->
    <data-box :data="dataSet">
      <template slot="columns">
        <el-table-column prop="caseNumber" label="借据号" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="contName" label="客户姓名" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="contEmail" label="邮件地址" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column label="操作" :min-width="$helper.getColumnWidth(4)">
          <template slot-scope="scope">
            <el-button type="text" @click="removeMail(scope)">移除</el-button>
          </template>
        </el-table-column>
      </template>
    </data-box>

    <!-- 邮件模板选择 -->
    <div style="margin-top:20px">
      <el-form :model="model" ref="form" label-width="75px">
        <el-form-item label="逾期阶段" prop="overduePeriod" :rules="{required: true, message: '请选择逾期阶段', trigger: 'blur'}">
          <el-select v-model="model.overduePeriod" placeholder="请选择" clearable @change="onOverduePeriodChange">
            <el-option v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')" :key="index" :label="name" :value="code.replace(/\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称" prop="templateId" :rules="{required: true, message: '请选择模板', trigger: 'blur'}">
          <el-select v-model="model.templateId" placeholder="请选择" clearable @change="onTemplateChange">
            <el-option v-for="item in templateSet" :key="item.id" :label="item.templateName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="抄送人">
          <el-input type="textarea" :maxlength="500" v-model="model.bccList" placeholder="多个抄送人以“ ; ”隔开" :rows="1"></el-input>
        </el-form-item>
        <el-form-item label="邮件内容" prop="messageContent">
          <el-input type="textarea" :maxlength="500" v-model="model.messageContent" readonly :rows="10"></el-input>
        </el-form-item>
      </el-form>
    </div>

    <div class="operate-buttons">
      <el-button @click="close()">取消</el-button>
      <el-button @click="submit()">发送</el-button>
    </div>
  </section>
</template>


<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies } from "~/core/decorator";
import { Emit, Prop, Model } from "vue-property-decorator";
import { State, namespace } from "vuex-class";
import DataBox from "~/components/common/data-box.vue";
import { SmartCollectionService } from "~/services/domain-service/smart-collection.service";
import { TemplateService } from "~/services/domain-service/template.service";

@Component({
  components: {
    DataBox
  }
})
export default class MailSendList extends Vue {
  @Dependencies(SmartCollectionService)
  private smartCollectionService: SmartCollectionService;
  @Dependencies(TemplateService)
  private templateService: TemplateService;
  @Prop()
  private selectionList: any[];

  private dataSet: any[] = [];

  private templateSet: any[] = [];

  private model: any = {
    overduePeriod: "",
    templateId: "",
    messageContent: "",
    bccList: ""
  };

  public refreshData() {
    const form: any = this.$refs["form"];
    form.resetFields();
    this.loadingData();
  }

  @Emit("close")
  private close() {}

  private loadingData() {
    const request: any = {
      ids: this.selectionList.map(v => v.id)
    };
    this.smartCollectionService
      .getMailPersonal(request)
      .subscribe(data => (this.dataSet = data || []));
  }

  /**
   * 逾期阶段改变
   */
  private onOverduePeriodChange(overduePeriod: string) {
    if (overduePeriod) {
      this.templateService
        .getTemplateByOverduePeriod({ overduePeriod, templateCategory: "MAIL" })
        .subscribe(data => (this.templateSet = data || []));
    } else {
      this.model.overduePeriod = "";
    }
    this.onTemplateChange("");
  }

  /**
   * 模板改变
   */
  private onTemplateChange(templateId: string) {
    let templateContent: string = "";
    if (templateId) {
      const template: any = this.templateSet.find(v => v.id === templateId);
      templateContent = template.templateContent;
    }
    this.model.templateId = templateId;
    this.model.messageContent = templateContent;
  }

  /**
   * 发送
   */
  private submit() {
    if (this.dataSet.length === 0) {
      this.$message.warning("数据为空,不能发送");
      return;
    }
    const form: any = this.$refs["form"];
    form.validate(v => {
      if (!v) return;
      const sendMessage: any[] = this.dataSet.map(v => {
        return {
          caseId: v.caseId,
          contId: v.id,
          // bccList: this.model.bccList.split(";").filter(x=> x!=='')
        };
      });
      const request: any = {
        templateId: this.model.templateId,
        sendMessageRequests: sendMessage,
        bccList: this.model.bccList.split(";").filter(x=> x!=='')
      };
      this.smartCollectionService.batchSendMail(request).subscribe(data => {
        this.close();
        this.$message.success("发送成功");
      });
    });
  }

  /**
   * 移除邮箱
   */
  private removeMail(scope: any) {
    const index: number = scope.$index;
    this.dataSet.splice(index, 1);
  }
}
</script>

